<!--
 * @Descripttion: 打印机列表
 * @Author: xiao li
 * @Date: 2020-09-30 10:31:48
 * @LastEditors: xiao li
 * @LastEditTime: 2021-10-08 16:44:38
-->
<template>
  <div class="lb-order-manage">
    <top-nav />
    <div class="page-main">
      <div class="flex-between mb-lg">
        <lb-button
          size="medium"
          type="primary"
          opType="add"
          icon="el-icon-plus"
          @click="
            $router.push(`/malls/editPrint?store_id=${searchForm.store_id}`)
          "
          >{{ $t('menu.MallsPrintAdd') }}</lb-button
        >
      </div>
      <el-table
        v-loading="loading"
        :data="tableData"
        :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
        style="width: 100%"
      >
        <el-table-column prop="id" label="ID" width="100"></el-table-column>
        <el-table-column prop="title" label="设备名称"></el-table-column>
        <!-- <el-table-column prop="type" label="设备品牌">
            <template slot-scope="scope">
              <div>{{scope.row.type === 1 ? '飞鹅' : '易联云'}}</div>
            </template>
        </el-table-column> -->
        <el-table-column prop="user" label="USER"></el-table-column>
        <el-table-column prop="api_key" label="UKEY"></el-table-column>
        <el-table-column prop="printer_key" label="SN"></el-table-column>
        <el-table-column prop="status" label="自动打印">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.auto"
              :active-value="1"
              :inactive-value="0"
              @change="toChangePrint(scope.row.id, scope.row.auto)"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <div class="table-operate">
              <lb-button
                size="mini"
                plain
                type="primary"
                @click="$router.push(`/malls/editPrint?id=${scope.row.id}`)"
                >{{ $t('action.edit') }}</lb-button
              >
              <lb-button
                size="mini"
                plain
                type="danger"
                @click="confirmDel(scope.row.id, -1)"
                >{{ $t('action.delete') }}</lb-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
      <lb-page
        :batch="false"
        :page="searchForm.page"
        :pageSize="searchForm.limit"
        :total="total"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
      >
      </lb-page>
    </div>
  </div>
</template>

<script>
// import pubic from '@/utils/public'
export default {
  data () {
    return {
      loading: false,
      searchForm: {
        page: 1,
        limit: 10,
        store_id: 0
      },
      tableData: [],
      total: 0,
      store_title: ''
    }
  },
  activated () {
    let { id = 0 } = this.$route.query
    this.searchForm.store_id = id
    this.getTableDataList()
  },
  methods: {
    handleSizeChange (val) {
      this.searchForm.limit = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.searchForm.page = val
      this.getTableDataList()
    },
    getTableDataList (flag) {
      if (flag) this.searchForm.page = 1
      this.loading = true
      let { searchForm } = this
      this.$api.mallsPrinterList(searchForm).then(res => {
        this.loading = false
        if (res.code === 200) {
          this.tableData = res.data.data
          this.total = res.data.total
          this.store_title = res.data.store_title
        }
      })
    },
    confirmDel (id, status) {
      this.$confirm(this.$t('tips.confirmDelete'), this.$t('tips.reminder'), {
        confirmButtonText: this.$t('action.comfirm'),
        cancelButtonText: this.$t('action.cancel'),
        type: 'warning'
      }).then(() => {
        this.toChangePrint(id, status, 1)
      }).catch(() => {

      })
    },
    async toChangePrint (id, status, key = 0) {
      let param = { id }
      if (key === 1) {
        param.status = status
      } else {
        param.auto = status
      }
      this.$api.mallsPrinterUpdate(param).then(res => {
        if (res.code === 200) {
          this.$message.success(this.$t(status === -1 ? 'tips.successDel' : 'tips.successOper'))
          if (status === -1) {
            this.searchForm.page = this.searchForm.page < Math.ceil((this.total - 1) / this.searchForm.limit) ? this.searchForm.page : Math.ceil((this.total - 1) / this.searchForm.limit)
            this.getTableDataList()
          }
        } else {
          if (status === -1) return
          this.getTableDataList()
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
